{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Image Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Table of Contents\n", "* [1. Image Functions](#1.-Image-Functions)\n", "\t* [1.1 Pixel-based Drawing](#1.1-Pixel-based-Drawing)\n", "\t* [1.2 Image Flipping](#1.2-Image-Flipping)\n", "\t\t* [1.2.1 Horizontal Mirror](#1.2.1-Horizontal-Mirror)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.1 Pixel-based Drawing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, let's draw an image that we create:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_1\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_1\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_1\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_1\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #1:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #1 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "PImage img;\n", "\n", "void setup() {\n", " size(500, 500);\n", " img = new PImage(200, 300, RGB);\n", "}\n", "\n", "void draw() {\n", " background(128);\n", " image(img, 0, 0);\n", " noLoop();\n", "}\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We know that we can create shapes using the built-in Processing functions like rect(). But what are they doing at the deepest level? Could we do it ourselves?" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_2\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_2\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_2\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_2\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #2:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #2 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "PImage img;\n", "\n", "void setup() {\n", " size(500, 500);\n", " img = new PImage(500, 500, RGB);\n", "}\n", "\n", "void draw() {\n", " myrect(10, 20, 200, 200, color(255, 0, 0));\n", " mycircle(300, 300, 75, color(0, 255, 0));\n", " image(img, 0, 0);\n", " noLoop();\n", "}\n", "\n", "void myrect(int x, int y, int w, int h, color c) {\n", " // what would go here?\n", "}\n", "\n", "void mycircle(int x, int y, int radius, color c) {\n", " // what would go here?\n", "}\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Yes, we could. All we need to do is change the pixels ourselves, like this:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_3\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_3\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_3\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_3\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #3:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #3 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "PImage img;\n", "\n", "void setup() {\n", " size(500, 500);\n", " img = new PImage(500, 500, RGB);\n", "}\n", "\n", "void draw() {\n", " myrect(10, 20, 200, 200, color(255, 0, 0));\n", " mycircle(300, 300, 75, color(0, 255, 0));\n", " image(img, 0, 0);\n", " noLoop();\n", "}\n", "\n", "void myrect(int x, int y, int w, int h, color c) {\n", " // what would go here?\n", " img.loadPixels();\n", " for (int i=x; i < x + w; i++) {\n", " for (int j=y; j < y + h; j++) {\n", " img.pixels[i + j * img.width] = c;\n", " }\n", " }\n", " img.updatePixels();\n", "}\n", "\n", "void mycircle(int x, int y, int radius, color c) {\n", " // what would go here?\n", " img.loadPixels();\n", " for (int i=x-radius; i < x + radius; i++) {\n", " for (int j=y-radius; j < y + radius; j++) {\n", " img.pixels[i + j * img.width] = c;\n", " }\n", " }\n", " img.updatePixels();\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**BONUS PROBLEM 1:**\n", "\n", "Write the code that draws a green circle rather than a green square." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.2 Image Flipping" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.2.1 Horizontal Mirror" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this demonstration, I'll use this picture of a student:\n", "\n", "\n", "\n", "You can download it, and upload it yourself, or use:\n", "\n", "```\n", "%download https://athena.brynmawr.edu/jupyter/hub/dblank/public/CS110%20Intro%20to%20Computing/2015-Fall/Notes/IMG_4688.jpeg\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, let's just test it and display it:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_16\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_16\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_16\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_16\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #16:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #16 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "/* @pjs preload=\"IMG_4688.jpeg\"; */\n", "\n", "PImage img;\n", "\n", "void setup() {\n", " img = loadImage(\"IMG_4688.jpeg\");\n", " img.resize(int(img.width/20), int(img.height/20));\n", " size(img.width, img.height);\n", " img.loadPixels();\n", "}\n", "\n", "void draw() {\n", " image(img, 0, 0);\n", " noLoop();\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, let's flip it so that everything on the right is on the left and everything on the left is on the right.\n", "\n", "To do that, we just map the pixels so that the pixels in the first column (zero) will now be in the last column (img.width - 1):" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_5\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_5\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_5\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_5\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #5:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #5 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "/* @pjs preload=\"IMG_4688.jpeg\"; */\n", "\n", "PImage img;\n", "PImage result;\n", "\n", "void setup() {\n", " img = loadImage(\"IMG_4688.jpeg\");\n", " img.resize(int(img.width/5), int(img.height/5));\n", " size(img.width, img.height);\n", " img.loadPixels(); \n", " // Create a result from img:\n", " result = new PImage(img.width, img.height);\n", " result.loadPixels();\n", " for (int y=0; y < img.height; y++) {\n", " for (int x=0; x < img.width; x++) {\n", " color c = img.pixels[x + y * img.width];\n", " result.pixels[img.width - 1 - x + (y * img.width)] = c;\n", " }\n", " } \n", " result.updatePixels();\n", "}\n", "\n", "void draw() {\n", " image(result, 0, 0);\n", " noLoop();\n", "}\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "**BONUS PROBLEM 2:**\n", "\n", "Flip student upside down. " ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_14\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_14\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_14\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_14\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #14:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #14 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "RGB: 165, 48, 74\n" ] } ], "source": [ "/* @pjs preload=\"IMG_4688.jpeg\"; */\n", "\n", "PImage img;\n", "\n", "void setup() {\n", " img = loadImage(\"IMG_4688.jpeg\");\n", " img.resize(int(img.width/5), int(img.height/5));\n", " size(img.width, img.height);\n", " img.loadPixels();\n", " for (int y=0; y < img.height; y++) {\n", " for (int x=0; x < img.width; x++) {\n", " color c = img.pixels[x + y * img.width];\n", " float r = red(c);\n", " float g = green(c);\n", " float b = blue(c);\n", " // 201, 59, 98\n", " if (190 < r && r < 250 &&\n", " 55 < g && g < 90 &&\n", " 90 < b && b < 150) {\n", " img.pixels[x + (y * img.width)] = color(0, 255, 0);\n", " }\n", " }\n", " } \n", " img.updatePixels(); \n", "}\n", "\n", "void mousePressed() {\n", " color c = img.get(mouseX, mouseY);\n", " float r = red(c);\n", " float g = green(c);\n", " float b = blue(c);\n", " println(\"RGB: \" + r + \", \" + g + \", \" + b);\n", "}\n", "\n", "void draw() {\n", " image(img, 0, 0);\n", " noLoop();\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Calysto Processing", "language": "java", "name": "calysto_processing" }, "language_info": { "codemirror_mode": { "name": "text/x-java", "version": 2 }, "file_extension": ".java", "mimetype": "text/x-java", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }